package com.sgg.datastructure.linklist;

import java.util.Stack;

public class SingleLinkList {
    public HeroNode head = new HeroNode(0, "", "");

    public void add(HeroNode heroNode) {
        HeroNode tmp = head;
        while (true) {
            if (tmp.next == null) {
                tmp.next = heroNode;
                break;
            }
            tmp = tmp.next;
        }
    }

    //按照编号no的顺序添加到指定位置
    //编号相同则提示
    public void addByOrder(HeroNode node) {
        boolean flag = false;
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == node.no) {
                flag = true;
                break;
            } else if (temp.next.no > node.no) {
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.printf("英雄编号%d已经重复了,不允许重复添加\n", node.no);
        } else {
            node.next = temp.next;
            temp.next = node;
        }
    }

    //修改
    public void update(HeroNode heroNode) {
        boolean flag = false;
        if (head.next == null) {
            System.out.println("链表为空,无法操作");
            return;
        }
        HeroNode temp = head;
        while (true) {
            temp = temp.next;
            if (temp.no == heroNode.no) {
                flag = true;
                break;
            }
            if (temp.next == null) {
                break;
            }
        }
        if (flag) {
            temp.name = heroNode.name;
            temp.nickname = heroNode.nickname;
        } else {
            System.out.printf("链表里面没有编号为%d的英雄\n", heroNode.no);
        }
    }

    //删除:找到相等的那个节点的上一个节点,把他的next=next.next
    public void delete(int no) {
        boolean flag = false;
        if (head.next == null) {
            System.out.println("链表为空,无法操作");
            return;
        }
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.next = temp.next.next;
        } else {
            System.out.printf("链表里面没有编号为%d的英雄\n", no);
        }
    }

    public void show() {
        if (head.next == null) {
            System.out.println("链表无数据");
            return;
        }
        HeroNode tmp = head.next;
        do {
            System.out.println(tmp);
            tmp = tmp.next;
        } while (tmp != null);
    }

    /**
     * 得到链表的长度
     */
    public int getSize() {
        if (head.next == null) {
            return 0;
        }
        HeroNode temp = head.next;
        int count = 0;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
        return count;
    }

    /**
     * 找到倒数第k个节点
     */
    public HeroNode findLastK(int k) {
        int size = getSize();
        if (k > size) {
            throw new RuntimeException("倒数的个数超过总容量,报错");
        }
        int index = size - k + 1;
        HeroNode temp = head;
        while (index > 0) {
            temp = temp.next;
            index--;
        }
        return temp;
    }

    /**
     * 反转 自己写的
     */
    public void reverse() {
        //除了头结点以外没有节点或只有1个节点,就不用反转
        if (head.next == null || head.next.next == null) {
            return;
        }
        HeroNode reverseNode = new HeroNode();
        HeroNode temp = head.next;
        while (temp != null) {
            HeroNode heroNode = new HeroNode(temp.no, temp.name, temp.nickname);
            heroNode.next = reverseNode.next;
            reverseNode.next = heroNode;
            temp = temp.next;
        }
        head.next = reverseNode.next;
    }

    /**
     * 老师的思路
     */
    public void reverseTeacher() {
        //除了头结点以外没有节点或只有1个节点,就不用反转
        if (head.next == null || head.next.next == null) {
            return;
        }
        HeroNode reverseNode = new HeroNode();
        HeroNode temp = head.next;
        HeroNode next = null;//保留下一个节点,要不然去操作temp.next后就丢了
        while (temp != null) {
            next = temp.next;
            temp.next = reverseNode.next;
            reverseNode.next = temp;
            temp = next;
        }
        head.next = reverseNode.next;
    }

    /**
     * 逆序打印单链表
     * 思路:不破坏原先的链表
     * 利用栈的后进先出的特性
     */
    public void reversePrint() {
        Stack<HeroNode> stack = new Stack<>();
        HeroNode temp = head.next;
        while (temp != null) {
            stack.add(temp);
            temp = temp.next;
        }
        while (stack.size() > 0) {
            System.out.println(stack.pop());
        }
    }

    /**
     * 合并2个有序的单链表
     * 要求结果也是有序的
     * 不会做 todo
     */
    public static SingleLinkList mergeOrderly(SingleLinkList list, SingleLinkList mergeList) {
//        HeroNode node = list.head.next;
//        HeroNode merge = mergeList.head.next;
//
//        HeroNode newHead = new HeroNode();
//        HeroNode current = newHead;
//        while (node != null || merge != null) {
//
//        }
//        SingleLinkList singleLinkList = new SingleLinkList();
//        singleLinkList.head.next = newHead.next;
        return null;
    }


    public static void main(String[] args) {
        SingleLinkList singleLinkList = new SingleLinkList();
        singleLinkList.add(new HeroNode(1, "宋江", "及时雨"));
        singleLinkList.add(new HeroNode(3, "吴勇", "智多星"));
        singleLinkList.add(new HeroNode(4, "aa", "dddd"));

        SingleLinkList m = new SingleLinkList();
        m.add(new HeroNode(2, "22", "及时雨"));
        m.add(new HeroNode(5, "55", "智多星"));
        m.add(new HeroNode(6, "66", "dddd"));

    }
}
